<?php

class WxNativePayController extends WechatAppController{
	
	public function beforeFilter(){
		parent::beforeFilter();
	    $this->Auth->allow('payback','perpay','wxreturn','paystate');
	}

	public function perpay($order_id = false){
		$this->loadModel('UserOrder');
		$order = $this->UserOrder->findById($order_id);
		$order_no = $order['UserOrder']['ordernum'];
		if($order['UserOrder']['user_order_state_id'] != 1){
			$this->redirect(array('plugin'=>'wechat','controller'=>'WxNativePay','action'=>'wxreturn','success'));
		}else{
			$param = array(
					'body'=>'和平票务-购票费用['.$order_no.']',
					'attach'=>'',
					'out_trade_no'=>$order['UserOrder']['ordernum'],
					'fee'=>$order['UserOrder']['price_total'],
					'tag'=>'',
					'product_id'=>$order['UserOrder']['id'],
					'notify'=>Router::url('/',true).'wechat/WxNativePay/payback'
			);
			$pre_order = $this->WxNativePay->getNative($param);
			if($pre_order['return_code'] == 'SUCCESS'){
				$state = true;
				$msg = $pre_order['code_url'];
			}else{
				$state = false;
				$msg = $pre_order['return_msg'];
			}
		}
		$this->set(compact('order','state','msg'));
	}
	
	public function paystate($order_id = false){
		$this->loadModel('UserOrder');
		$order = $this->UserOrder->findById($order_id);
		if($order['UserOrder']['user_order_state_id'] != 1){
			$state = true;
			$msg = 'success';
		}else{
			$state = false;
			$msg = 'unpaid';
		}
		$this->set(compact('state','msg'));
		$this->render(false);
	}
	
    public function payback() {
        $this->AutoRender = false;
        $this->layout = false;
        $notify = new PayNotifyCallBack();
        $notify->Handle(false);
        $this->render(false);
	}
	
	function wxreturn($msg = ''){
		$state = true;
		if($msg == 'success'){
			$message = '您的订单已成功支付';
		}else if($msg == 'success_already'){
			$message = '您的订单已经成功支付';
		}else{
			$state = false;
			$message = '订单支付失败';
		}
	
		$this->set('seo_title','支付结果');
		$this->set('seo_keywords',false);
		$this->set('seo_description',false);
		$this->set('seo_title_ext','【和平票务订票官网】');
	
		$this->set(compact('message','state'));
	}
}

App::import('Vendor','Wechat.WxPayApi',array('file'=>'wxpay'.DS.'WxPay.Api.php'));
App::import('Vendor','Wechat.WxPayNotify',array('file'=>'wxpay'.DS.'WxPay.Notify.php'));
class PayNotifyCallBack extends WxPayNotify
{
    //查询订单
    public function Queryorder($transaction_id)
    {
        $input = new WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = WxPayApi::orderQuery($input);
        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS")
        {
            return true;
        }
        return false;
    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        App::import('Model','UserOrder');
        $OrderObj = new UserOrder();
        $serial = $data['out_trade_no'];
        $OrderObj->recursive = -1;
        $order = $OrderObj->findByOrdernum($serial);
    	if($order['UserOrder']['user_order_state_id'] == 1){
	    	$order_id = $order['UserOrder']['id']; 
	    	$res = $OrderObj->updateOrderState($order_id,2,6,'【微信扫码支付】'.$data['transaction_id']);
    	}else{
    		$res = false;
    	}
        if($res){
            return true;
        }else{
            return false;
        }
    }
}
